//
//  ZyaBillController.swift
//  Slaughter
//  Created by 邱仙凯 on 2018/8/10.
//  Copyright © 2018年 zwb. All rights reserved.
//

import UIKit

class ZyaBillController: UITableViewController {
    
    var dataArray = [ZyaBillModel]()
    var startDate = dateOfThisMonth()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "对账单列表"
        tableView.mj_header = KSRefreshHeader {
            self.fetchData()
        }
        tableView.mj_header!.beginRefreshing()
        tableView.tableFooterView = UIView()
        fetchData()
    }
    @IBAction func searchAction(_ sender: Any) {
        
        let alert = KSAlertView()
        let txt0 = alert.addPicker(withMonth: "年月", action: nil)
        txt0.text = self.startDate
        _ = alert.addButton("查询") { [weak self] in
            let start = txt0.text!
            self!.startDate = start
            self!.tableView.mj_header!.beginRefreshing()
        }
        alert.show("查询条件", duration: 0.2, completeText: nil)
        
    }
    
    
    func fetchData() {
        KSNetwork.shared.doRequest(API.getCheckingBill(userId: USERINFO.usrid.unwrap, time: startDate), showHud: false) { (data:Result42Array<ZyaBillModel>) in
             self.dataArray.removeAll()
     
            self.tableView.mj_header?.endRefreshing()
            self.tableView.mj_footer?.endRefreshing()

            if let item = data.info {
               let arr = item.flatMap {$0}
                arr.forEach {
                    self.dataArray.append($0)
                }
                 self.tableView.reloadData()
                if item.count<1 {
                    self.tableView.mj_footer!.endRefreshingWithNoMoreData()
                }
            }
            
        }
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        guard let cell = tableView.dequeueReusableCell(withIdentifier: ZyaBillCell.identifier) as? ZyaBillCell else {
            fatalError("Could not create Cell")
        }
        cell.one = self.dataArray[indexPath.section]
        
        return cell
    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
       let one =   self.dataArray[indexPath.section]
        guard one.sMonth.unwrap.count > 5 else {
            KSToast("日期有误")
            return
        }
        
        let webview = ZyaZhangWebController()
        webview.loadUrl = "\(HTTPStr)DirectReport/\(TOKEN)?reportlet=sale/cwt_sale_dz_batch.cpt&p_client_id=\(one.sClientId.unwrap)&p_org_code=\(one.orgCode.unwrap)&p_start_date=\(one.sMonth.unwrap)-01&p_end_date=\(one.sMonth.unwrap)-\(numberOfdayInMonth(one.sMonth.unwrap))&p_month=\(one.sMonth.unwrap)&op=h5"
        webview.title = "报表"
        webview.one = one
        self.navigationController?.pushViewController(webview, animated: true)
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return dataArray.count
    }
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 120
    }
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 8
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 1
    }
    
}
